package com.miracle.flexAPI.api.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.miracle.flexAPI.api.entity.SysDatasource;
import com.miracle.flexAPI.api.mapper.SysDatasourceMapper;
import com.miracle.flexAPI.api.service.SysDatasourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.*;
import java.util.List;

/**
 * 数据源管理(SysDatasource)表服务实现类
 *
 * @author miracle
 * @since 2021-08-13 13:33:31
 */
@Slf4j
@Service("sysDatasourceService")
public class SysDatasourceServiceImpl implements SysDatasourceService {
    @Resource
    private SysDatasourceMapper sysDatasourceDao;

    @Autowired
    DynamicRoutingDataSource dynamicRoutingDataSource;


    /**
     * 查询列表
     *
     * @param model 实例对象
     * @return 对象列表
     */
    @Override
    public AjaxResult list(SysDatasource model) {
        List<SysDatasource> list = this.sysDatasourceDao.queryByDto(model);
        return AjaxResult.success(list);
    }

    /**
     * 新增数据
     *
     * @param model 实例对象
     * @return 实例对象
     */
    @Override
    public AjaxResult add(SysDatasource model) {
        model.setIsLoad(0);
        int result = this.sysDatasourceDao.insertByModel(model);
        return AjaxResult.success(result);
    }

    /**
     * 修改数据
     *
     * @param model 实例对象
     * @return 实例对象
     */
    @Override
    public AjaxResult edit(SysDatasource model) {
        model.setIsLoad(0);
        int result = this.sysDatasourceDao.updateByModel(model);
        if(result > 0){
            dynamicRoutingDataSource.removeDataSource(model.getVcSourceCode());
        }
        return AjaxResult.success(result);
    }

    /**
     * 删除数据
     *
     * @param model 实例对象
     * @return 删除数量
     */
    @Override
    public AjaxResult delete(SysDatasource model) {
        int result = this.sysDatasourceDao.deleteByModel(model);
        if(result > 0){
            dynamicRoutingDataSource.removeDataSource(model.getVcSourceCode());
            //log.info("dynamicRoutingDataSource edit delete :" + model.getVcSourceCode());
        }
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult loadByCode(SysDatasource model) {
        model.setIsLoad(0);
        List<SysDatasource> list = this.sysDatasourceDao.queryByDto(model);
        if(list.size() > 0){
            SysDatasource data = list.get(0);
            String vcUrl = data.getVcUrl();
            String vcUser = data.getVcUser();
            String vcPwd = data.getVcPwd();
            String vcDriver = data.getVcDriver();
            try {
                Class.forName(vcDriver);
                Connection conn = DriverManager.getConnection(vcUrl, vcUser, vcPwd);
                if(conn != null){
                    DruidDataSource druidDataSource = new DruidDataSource();
                    druidDataSource.setUrl(vcUrl);
                    druidDataSource.setUsername(vcUser);
                    druidDataSource.setPassword(vcPwd);
                    druidDataSource.setDriverClassName(vcDriver);
                    dynamicRoutingDataSource.addDataSource(model.getVcSourceCode(),druidDataSource);
                    model.setIsLoad(1);
                    this.sysDatasourceDao.updateByModel(model);//修改为已加载
                }
            } catch (SQLException e) {
                return AjaxResult.success(e.getMessage());
            } catch (ClassNotFoundException e) {
                return AjaxResult.success(e.getMessage());
            }
        }else{
            return AjaxResult.success("查询不到数据源");
        }
        return AjaxResult.success("加载成功");
    }

    @Override
    public AjaxResult unloadByCode(SysDatasource model) {
        dynamicRoutingDataSource.removeDataSource(model.getVcSourceCode());
        model.setIsLoad(0);
        this.sysDatasourceDao.updateByModel(model);//修改为未加载
        return AjaxResult.success("卸载成功");
    }
}
